%This script creates 4 distinct clusters
%All algorithms should work well on them

delta = 5;
cluster_size = 1000;
std_dev = 1;
C1 = create_cluster([delta, delta]', [std_dev,std_dev]', cluster_size);
C2 = create_cluster([-delta, delta]', [std_dev,std_dev]', cluster_size);
C3 = create_cluster([-delta, -delta]', [std_dev,std_dev]', cluster_size);
C4 = create_cluster([delta, -delta]', [std_dev,std_dev]', cluster_size);

C = [C1, C2, C3, C4];

%The results
my_ones = ones(1,cluster_size);
generated_class = [my_ones, my_ones*2, my_ones*3, my_ones*4];
scatter(C(1,:), C(2,:), [], generated_class', 'filled');
axis equal;
title('Generated data set')

%K-means
figure;
class = kmeans(C', 4, 'replicates', 6);
scatter(C(1,:), C(2,:),[], class', 'filled');
axis equal;
rand_index = RandIndex(generated_class, class);
t = sprintf('K-means results\n Rand Index = %d', rand_index);
title(t);

%Hirerarchical
figure;
class = clusterdata(C', 4);
scatter(C(1,:), C(2,:),[], class', 'filled');
axis equal;
rand_index = RandIndex(generated_class, class);
t = sprintf('Hierarchical results\n RandIndex = %d', rand_index);
title(t);

%Results - one run of K-means can result in converging to a local
%minimum/maximum and may lead to wrong result
%when the clusters are close - the Hierarchical yields poor results
%Hierarchical is slower